﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;

namespace QuickExcel
{
    public partial class ExportRangeToFile : Form
    {
        public ExportRangeToFile()
        {
            Excel.Range Selection = Globals.ThisAddIn.Application.Selection as Excel.Range;
            InitializeComponent();
            txtRange.Text = Selection.Address;

        }
        private void txtRange_Click(object sender, EventArgs e)
        {
            Excel.Range rng;
            rng = Globals.ThisAddIn.Application.InputBox("Please select range", "Select Range", txtRange.Text, this.Left - 5, this.Top + 5, Type: 8);
            rng.Select();
            txtRange.Text = rng.Address;
            this.Activate();
        }

        private void opExcelWorkbook_CheckedChanged(object sender, EventArgs e)
        {
            if (opExcelWorkbook.Enabled == true)
            {
                chkFortoValue.Visible = true;
                chkPreserveFormat.Visible = true;
            }
            else
            {
                chkFortoValue.Visible = false;
                chkPreserveFormat.Visible = false;
            }
        }

        private void opTextFile_CheckedChanged(object sender, EventArgs e)
        {
            if (opTextFile.Checked == false)
            {
                chkFortoValue.Visible = true;
                chkPreserveFormat.Visible = true;
            }
            else
            {
                chkFortoValue.Visible = false;
                chkPreserveFormat.Visible = false;
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            Excel.Range rng = Globals.ThisAddIn.Application.ActiveSheet.Range(txtRange.Text);

            if (rng.Areas.Count > 1)
                System.Windows.Forms.MessageBox.Show("This utility does not work with multiple sections. Pls Try Again");

            if (opExcelWorkbook.Checked == true)
            {
                Excel.Workbook wb;

                wb = (Excel.Workbook) Globals.ThisAddIn.Application.Workbooks.Add();
                rng.Copy();

                wb.Sheets[1].Range["A1"].PasteSpecial(Excel.XlPasteType.xlPasteColumnWidths);
                wb.Sheets[1].Range["A1"].PasteSpecial(Excel.XlPasteType.xlPasteAll);
               
                if (chkFortoValue.Checked)
                    wb.Sheets[1].Range["A1"].PasteSpecial(Excel.XlPasteType.xlPasteValues);
                
                if (!chkPreserveFormat.Checked)
                    wb.Sheets[1].UsedRange.ClearFormats();

                if (!chkOpenFile.Checked)
                {
                    saveFileAs.Filter = "Excel Workbook | *.xlsx";
                    saveFileAs.ShowDialog();
                    wb.SaveAs(saveFileAs.FileName);
                }
            }
            else
            {
                string file;
                Excel.Worksheet wsh;
                wsh = Globals.ThisAddIn.Application.ActiveWorkbook.Sheets.Add();
                rng.Copy();
                wsh.Range["A1"].PasteSpecial(Excel.XlPasteType.xlPasteValues);

                Globals.ThisAddIn.Application.DisplayAlerts = false;
                wsh.Delete();
                Globals.ThisAddIn.Application.DisplayAlerts = true;

                file = "Book1.csv";
                wsh.SaveAs(file,Excel.XlFileFormat.xlCSV);

                if (!chkOpenFile.Checked)
                {
                    Globals.ThisAddIn.Application.Workbooks.Open(file);
                }
            }
        }

    }
}
